home *** CD-ROM | disk | FTP | other *** search
/ Java 1996 August / Java - Summer 1996.iso / windows / doc / inputfil.jav < prev    next >
Encoding:
Text File  |  1996-02-26  |  2.7 KB  |  82 lines

  1. /*
  2.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  3.  *
  4.  * Permission to use, copy, modify, and distribute this software
  5.  * and its documentation for NON-COMMERCIAL purposes and without
  6.  * fee is hereby granted provided that this copyright notice
  7.  * appears in all copies. Please refer to the file "copyright.html"
  8.  * for further important copyright and licensing information.
  9.  *
  10.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  11.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  12.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  13.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  14.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  15.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  16.  */
  17. /*
  18.  * This file extends the File class by adding file manipulation
  19.  * methods creating a read-only input file abstraction. We leverage
  20.  * the path manipulation code provided in the File superclass and
  21.  * extend that code with support for opening and reading files.
  22.  */
  23.  
  24. /**
  25.  * Define class InputFile that presents a simple read-only input file 
  26.  * abstraction. Note that we use native or non-Java methods to
  27.  * implement some of the methods.
  28.  */
  29. public
  30. class InputFile extends File {
  31.  
  32.     /**
  33.      * Link in the native library that we depend on.  If we cannot
  34.      * link this in, an exception is generated and the class loading
  35.      * fails. We have arbitrarily named the library "file" at the
  36.      * Java level (or libfile.so at the solaris level). Additionally,
  37.      * the System call is part of the static initializer for the class.
  38.      * Thus, the library is loaded as part of this class being loaded.
  39.      */
  40.     static {
  41.     try {
  42.             System.loadLibrary("file");
  43.     } catch (UnsatisfiedLinkError e) {
  44.             System.out.println("can't find your library");
  45.             System.exit(-1);
  46.         }
  47.  
  48.     }
  49.  
  50.     /**
  51.      * Holds the system dependent handle to the file resource.
  52.      */
  53.     protected int fd;
  54.  
  55.     /**
  56.      * Constructor for the input file object. Initializes the
  57.      * parent class with the path name.
  58.      */
  59.     public InputFile(String path) {
  60.     super(path);
  61.     }
  62.  
  63.     /**
  64.      * Attempts to open the file for reading. Returns
  65.      * TRUE on success and FALSE on failure. Alternatively, we could
  66.      * throw an exception and catch it.
  67.      */
  68.     public native boolean open();
  69.  
  70.     /**
  71.      * Attempts to close the previously opened file. Has
  72.      * no return value.
  73.      */
  74.     public native void close();
  75.  
  76.     /**
  77.      * Reads some number of bytes from the opened file. Returns
  78.      * the number of bytes read or -1 when the file is empty.
  79.      */
  80.     public native int read(byte b[], int len);
  81. }
  82.